#!/bin/bash

#############################################################################
#This function should be included anywhere you want to source seautils script
function _include()
{
    # $1 is the sh file that you want to source
    AutoDIR=`pwd | awk -F '/' '{\
        for (i=1;i<=NF;i++)\
        {\
            printf("%s/", $i);\
            if ($i=="scripts") break\
        }\
        printf("\n")\
    }'`
    ScriptName=`find $AutoDIR -name "$1"`
    
    if [ ! -z "$ScriptName" ]
    then
        echo "Import $ScriptName"
        . $ScriptName
    fi
}
#############################################################################


_include "check.sh"

WaitSecs="3"

function enable_pegasus_log()
{
    LogDIR="/var/lib/Pegasus/cache"

    /etc/init.d/tog-pegasus start
    sleep $WaitSecs

    echo "Enabling OpenPegasus Logging ...."
    cimconfig -c -s traceLevel=4
    cimconfig -c -s traceComponents=ALL
    sleep $WaitSecs
    
    echo "Enabling SMX Logging ...."
    cimprovider -l | awk '{ gsub(/Module$/, "", $0); print $0 }'  | while read ProviderName
    do
        echo "Enable $ProviderName ...."
        touch /tmp/$ProviderName.text
    done
    echo "Done! restart cimserver to enable loging ...."
    
    /etc/init.d/tog-pegasus stop
    sleep $WaitSecs
    
    /etc/init.d/tog-pegasus start
    sleep $WaitSecs
    
    echo ""
    echo "Generating SMX Provider Log Files in: $LogDIR"
    ls $LogDIR
    
    echo ""
    echo "Generating Pegasus Log Files in: $LogDIR/trace"
    ls "$LogDIR/trace"

}

function disable_pegasus_log()
{
    LogDIR="/var/lib/Pegasus/cache"

    /etc/init.d/tog-pegasus start
    sleep $WaitSecs
    
    echo "Disabling OpenPegasus Logging ...."
    cimconfig -c -u traceComponents
    cimconfig -c -u traceLevel
    sleep $WaitSecs
    
    echo "Disabling SMX Logging ...."
    rm -f /tmp/*.text
    
    echo "Done! restart cimserver to disable loging ...."
    
    /etc/init.d/tog-pegasus stop
    sleep $WaitSecs
    
    /etc/init.d/tog-pegasus start
    sleep $WaitSecs
    
    echo ""
    echo "Cleaning SMX Provider Log Files in: $LogDIR"
    rm -f $LogDIR/*.log
    
    echo ""
    echo "Cleaning Pegasus Log Files in: $LogDIR/trace"
    rm -f $LogDIR/trace/*.trc*
}

#_detect_platform
_assert_param 1:$1 Range:enable,disable

if [ "$1" == "enable" ]; then
    enable_pegasus_log
fi

if [ "$1" == "disable" ]; then
    disable_pegasus_log
fi

